/**
* POI感应器，songxg，2011/1/18
*/

function POIInductor(opt) {
	// opt = {scales/resolution :[ ], usable:true, map:SuperMap.Web.Map, tileSize:256, url: url}
	var option = this._extend(opt);
	this._map = opt.map;
	this._poiTileList = new POITileList();
	this._url = opt.url;

	this._tileSize = opt.tileSize;
	//感应容限
	this._tolerance = {'w': 4, 'h': 4};
	//添加鼠标移动监听
	this._mouseMoveDelegate = Function.createDelegate(this, this._listenMouseMove);
	this._map.add_mouseMove(this._mouseMoveDelegate);
}
POIInductor.prototype = {
	_extend:function(opt) {
		if(!opt.map) {
			throw new Error("Compass's map is null!");
		}
		if(!opt.useable) {
			opt.useable = true;
		}
		//if((!opt.scales || opt.scales.length == 0) || (!opt.resolutions || opt.resolutions.length == 0)) {
			
		//}
		if(!opt.tileSize) {
			opt.tileSize = 256;
		}
		//if(!opt.url) {
		//	throw Error("POIInductor url is null");
		//}
	},
	
	//监听鼠标移动
	//1、根据鼠标位置算出鼠标索引位置
	//2、判断当前鼠标所在索引是否已经存在
	//3、如果不存在则发送请求
	_listenMouseMove:function(e) {
		var indexObj = this._computePosIndex(e.pixelCoord);
		//记录鼠标位置
		this._mousePosition = e.pixelCoord;
		
		if(!this._poiTileList._haveIndexObj(indexObj)) {
			//如果当前列表中无此索引，则根据索引进行请求，回调后将其加入列表中
			
			//:提交请求
			//SuperMap.Web.Service.Committer.REST(uri, 'GET', null, ["point2Ds", "unit"], [pointsStr, SuperMap.Web.Utility.getUnit(unit).toUpperCase()], onComplete, onError, userContext);
			var onComplete = Function.createDelegate(this, this._getTilePoiComplete);
			var onError = Function.createDelegate(this, this._getTilePoiError);
			SuperMap.Web.Service.Committer.REST(this._url, 'GET', null, ["indexX", "indexY", "indexZ"], [indexObj.indexX, indexObj.indexY, indexObj.indexZ], onComplete, onError, indexObj);
		}
		else {
			//如果当前列表中存在此索引，则取出
			var item = this._poiTileList.find(indexObj);
			
			//todo:响应处理
			this._inductor(item);
		}
	},
	
	//获取POI完成事件
	//将POI加入到列表中，并做相应处理
	_getTilePoiComplete:function(jsonObj, userContext) {
		//userContext为key
		//jsonObj为value
		var item = new POITileItem({'values': jsonObj, 'key': userContext});
		//item.values.push(jsonObj);
		this._poiTileList.add(item);
		
		//todo:响应处理
		this._inductor(item);
	},
		
	//感应
	//传入坐标，感应坐标所在POITileItem
	_inductor:function(item) {
		var inductorItem = item.inductor(this._mousePosition, this._map, this._tolerance);
		//cursor备份
		if(inductorItem) {
			//debugger;
			this._map.get_element().style.cursor='pointer';
			
		}
		else {
			this._map.get_action().resetCursor();
			console.log("not found poi");
		}
	},
	
	_getTilePoiError:function() {
		
	},
	
	//根据鼠标位置计算，当前鼠标所在的范围的索引
	//return object{x:1, y: 1 scale:14335435}
	//mousePos为鼠标当前位置的像素坐标
	_computePosIndex:function(mousePos) {
		var origin = this._map._getBoundsOriginPos();
		var indexX = Math.floor((mousePos.x - origin.x) / this._tileSize);
		var indexY = Math.floor((mousePos.y - origin.y) / this._tileSize);
		//目前只使用scale——level
		var indexZ = this._map.get_level();
		return {"indexX": indexX, "indexY": indexY, "indexZ": indexZ};
	}
};

function POITileList() {
	//数组成员为POITileItem
	//this._POITiles = new Array();
	//示例数据-》[{'id':"test", 'pos':{'x':12993071, 'y':5165920}, 'caption':{'title':"inductor success", 'snippet':"indector success"}}]
	this._POITiles = [new POITileItem({'values': [{'id':"test", 'pos':{'x':12993071, 'y':5165920}, 'caption':{'title':"inductor success", 'snippet':"indector success"}}], 'key': {'indexX': 1, "indexY": 0, "indexZ": 1}})];
}
POITileList.prototype = {
	//添加POITileItem
	//参数：POITileItem
	add:function(item) {
		this._POITiles.push(item);
	},
	
	//查找列表中POITileItem，找到则返回对象，没找到则返回null
	find:function(indexObj) {
		for(var i = 0; i < this._POITiles.length; i++) {
			if(this._POITiles[i].equals(indexObj)) {
				return this._POITiles[i];
			}
		}
		return null;
	},
	
	//判断当前indexObj是否存在，存在返回true，否则返回false
	//参数{indexX: indexX, "indexY": indexY, "indexZ": indexZ}
	_haveIndexObj:function(indexObj) {
		for(var i = 0; i < this._POITiles.length; i++) {
			if(this._POITiles[i].equals(indexObj)) {
				return true;
			}
		}
		return false;
	}
};

var IndexObj = {
	"indexX" : null,
	"indexY" : null,
	"indexZ" : null
}

function POITileItem(opt) {
	//key
	//IndexObj {indexX: indexX, "indexY": indexY, "indexZ": indexZ}
	this.key = opt.key;
	//values
	//jsonp
	//{id:"", pos:{x':x, 'y':y}, caption:{title:"", snippet:""}}
	this.values = opt.values;
	//this.values = [];
}
POITileItem.prototype = {
	//获取POITile访问地址
	//参数{indexX: indexX, "indexY": indexY, "indexZ": indexZ}
	getPOITileUrl:function(url) {
		//http://mt0.google.com/vt/ft?lyrs=lmq:1001:%E9%A4%90%E9%A5%AE|cc:CN|h:b|s:115968771510351694523&las=vwuvttvtwuwvvwutu&z=16&gl=cn&hl=zh-CN&xc=1&callback=_xdc_._mgj5ajrbo
		return url + "x=" + this.key.indexX + "&y=" + this.key.indexY + "&scale=" + this.key.indexZ + "&callback=_songxg_poiinductor";
	},
	//判断POIMessage是否相等，相等返回true，否则返回false
	equals:function(indexObj) {
		if(this.key.indexX == indexObj.indexX && this.key.indexY == indexObj.indexY && this.key.indexZ == indexObj.indexZ) {
			return true;
		}
		else {
			return false;
		}
	},
	
	//鼠标点感应
	//pos: 鼠标像素坐标
	//map: 地图参数，用于将地理坐标转换为像素坐标
	inductor:function(pos, map, tolerance) {
		for(var i = 0; i < this.values.length; i++) {
			var poiPos = map.mapToScreen(new SuperMap.Web.Core.Point2D(this.values[i].pos.x, this.values[i].pos.y));
			var a = tolerance.w / 2;
			var b = tolerance.h / 2;
			var range = new SuperMap.Web.Core.Rectangle(poiPos.x - a, poiPos.y - b, poiPos.x + a, poiPos.y + b);
			if(range.contains(pos.x, pos.y)) {
				return this.values[i];
			}
		}
		return false;	//无感应返回false
	}
		//features:[{id:"10212322405016741555",a:[0,0],bb:[-4,-4,4,4],c:"{1:{title:\"码头人家\",review_count:0}}"},{id:"2531682522874463447",a:[-516096,-385024],bb:[-4,-4,4,4],c:"{1:{title:\"燕春饭店\",review_count:0}}"},{id:"151497501716559081",a:[-311296,16384],bb:[-4,-4,4,4],c:"{1:{title:\"Tujia Diaozha Cake\",review_count:0}}"},{id:"15962013105082456216",a:[-458752,-442368],bb:[-4,-4,4,4],c:"{1:{title:\"北京圣聚城大酒店有限公司\",review_count:0}}"}]
	//toPOIItemList:function(json) {
		//this.POIList = json;
	//}
};